<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item :label="$t('equipment.name')" prop="name">
        <el-input
            v-model="queryParams.name"
            :placeholder="$t('equipment.nameInput')"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('equipment.style')" prop="type">
        <el-select v-model="queryParams.type" :placeholder="$t('equipment.styleInput')">
          <el-option
              v-for="dict in typeOptions"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('terminal.organization')" prop="deptId">
        <treeselect v-model="queryParams.deptId" :options="deptOptions" :default-expand-level="2"
                    :normalizer="deptNormalizer" style="width: 198px;" placeholder="请选择组织机构"
        />
      </el-form-item>
      <el-form-item :label="$t('equipment.onlineStatus')" prop="online">
        <el-select v-model="queryParams.online" :placeholder="$t('equipment.onlineStatusInput')">
          <el-option key="0" :label="$t('equipment.isUnderline')" value="0">{{
              $t('equipment.isUnderline')
            }}
          </el-option>
          <el-option key="1" :label="$t('equipment.isOnline')" value="1"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{
            $t('elButton.search')
          }}
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('elButton.reset') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            v-hasPerm="['deviceStat_export']"
            type="warning"
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
        >{{ $t('elButton.export') }}
        </el-button>
      </el-col>
      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" :content="$t('elButton.refresh')" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="handleQuery"/>
        </el-tooltip>
        <el-tooltip class="item" effect="dark"
                    :content="showSearch ? $t('elButton.hideSearch') : $t('elButton.showSearch')" placement="top"
        >
          <el-button size="mini" circle icon="el-icon-search" @click="showSearch=!showSearch"/>
        </el-tooltip>
      </div>
    </el-row>

    <el-table v-loading="loading" :data="deviceList" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column :label="$t('equipment.identification')" align="center" prop="code"
                       :show-overflow-tooltip="true"
      />
      <el-table-column :label="$t('equipment.name')" align="center" prop="name" :show-overflow-tooltip="true"/>
      <el-table-column :label="$t('equipment.sort')" align="center" prop="groupName"/>
      <el-table-column
          :label="$t('equipment.playStyle')"
          align="center"
          prop="playMode"
          :formatter="playModeFormat"
          :show-overflow-tooltip="true"
      />
      <el-table-column :label="$t('equipment.style')" align="center" prop="type" :formatter="typeFormat"/>
      <el-table-column :label="$t('equipment.resolving')" align="center" prop="resolution"/>
      <el-table-column :label="$t('equipment.ip')" align="center" prop="ipAddress" :show-overflow-tooltip="true"/>
      <el-table-column :label="$t('equipment.edition')" align="center" prop="softVersion"/>
      <el-table-column :label="$t('elButton.establishTime')" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('equipment.onlineStatus')" align="center" prop="online">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.online === '0'" type="danger">{{ $t('equipment.isUnderline') }}</el-tag>
          <el-tag v-if="scope.row.online === '1'" type="success">{{ $t('equipment.isOnline') }}</el-tag>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.current"
        :limit.sync="queryParams.size"
        @pagination="getList"
    />

  </div>
</template>

<script>
import {
  listDevice,
  exportDevice
} from '@/api/ips/device'
import { deptTree } from '@/api/system/dept'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'DeviceStat',
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 设备统计表格数据
      deviceList: [],
      // 机构名称树选项
      deptOptions: undefined,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      typeOptions: [], // 状态字典
      playModeOptions: [], // 播放模式
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        name: undefined,
        type: undefined,
        online: undefined
      },
      // 显示搜索条件
      showSearch: true,
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList()
    this.getDicts('device_type').then(response => {
      this.typeOptions = response.data
    })
    this.getDicts('play_mode').then(response => {
      this.playModeOptions = response.data
    })
    this.getDeptTree()
  },
  methods: {
    /** 查询设备统计列表 */
    getList() {
      this.loading = true
      listDevice(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.deviceList = response.data
        this.total = response.total
        this.loading = false
      })
    },
    /** 转换机构数据结构 */
    deptNormalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.deptId,
        label: node.name,
        children: node.children
      }
    },
    /** 查询机构名称下拉树结构 */
    getDeptTree() {
      deptTree().then(response => {
        if (response.data && response.data.length > 0) {
          this.deptOptions = this.handleTree(response.data, 'deptId', 'parentId', 'children', response.data[0].parentId)
        } else {
          this.deptOption = []
        }
      })
    },
    // 设备类型字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.type)
    },
    // 播放模式字典翻译
    playModeFormat(row, column) {
      return this.selectDictLabel(this.playModeOptions, row.playMode)
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      if (this.ids.length === 0) {
        this.msgWarning(this.$t('equipment.equipmentInput'))
        return
      }
      const queryParams = this.queryParams
      this.$confirm('是否确认导出选中的设备统计数据项?', this.$t('warning'), {
        confirmButtonText: this.$t('submitText'),
        cancelButtonText: this.$t('cancelText'),
        type: 'warning'
      }).then(() => {
        return exportDevice(queryParams, this.ids)
      }).then(response => {
        this.download(response.data)
      }).catch(function() {
      })
    }
  }
}
</script>
